
const State = cc.Enum({
    //游戏开始前的准备
    Ready:-1,
    //小鸟上升中
    Rise:-1,
    //小鸟自由落体
    FreeFall:-1,
    //碰撞钢管坠落
    Drop:-1,
    //坠落地面静止
    Dead:-1,
});
cc.Class({
    extends: cc.Component,
    statics:{
        State:State
    },
    properties: {
       
        initRiseSpeed:800,
        gravity:1000,
        ground:{
            default:null,
            type:cc.Node
        },
        state:{
            default:State.Ready,
            type:State,
        }
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    start () {

    },
    init(game){
        this.game=game;
        this.state=State.Ready;
        this.currentSpeed=0;
        this.anim = this.getComponent(cc.Animation);
    },
    update (dt) {
        if(this.state===State.Ready||this.state===State.Dead){
            return;
        }
        this.updatePosition(dt);
        this.updateState(dt);
        this.detectCollision();
        this.fixBirdFinalPosition();
    },
    updatePosition(dt){
        var flying = this.state === State.Rise || this.state === State.FreeFall || this.state === State.Drop;
        if(flying){
            var h= cc.director.getVisibleSize().height / 2;
            if(this.node.y>h){
                this.node.y = h-1;
                this.currentSpeed = -1;
            }else{
                this.currentSpeed -= dt * this.gravity;
                this.node.y += dt * this.currentSpeed;
            }
        }
    },
    updateState(dt){
        switch(this.state){
            case State.Rise:
                if(this.currentSpeed<0){
                    this.state = State.FreeFall;
                    this.runFallAction();
                }
                break;
        }
    },
    rise(){
        console.log('135')
        this.state = State.Rise;
        this.runRiseAction();
        this.currentSpeed = this.initRiseSpeed;
    },
    startFly(){
        this.getNextPipe();
        console.log('134')
        this.anim.stop('birdFlapping');
        this.rise();
    },
    runRiseAction(){
        this.node.stopAllActions();
        let jumpAction = cc.rotateTo(0.3,-30).easing(cc.easeCubicActionOut());
        this.node.runAction(jumpAction);
    },
    runFallAction(duration =0.6){
        let dropAction = cc.rotateTo(duration,90).easing(cc.easeCubicActionIn());
        this.node.runAction(dropAction);

    },
    runDropAction(){
        if(this.currentSpeed > 0){
            this.currentSpeed = 0;
        }
        this.runFallAction(0.4);
    },
    getNextPipe(){
        this.nextPipe = this.game.pipeManager.getNext();
    },
    detectCollision(){
        if(!this.nextPipe){
            console.log("返回退出")
            return;
        }
        if(this.state===State.Ready||this.state===State.Dead||this.state===State.Drop){
            return;
        }
        let collideWithPipe=false;
        //检测小鸟与上方管子的碰撞
        if(this.detectCollisionWithBird(this.nextPipe.topPipe)){
            console.log("与上方碰撞")
            collideWithPipe=true;
        }
        //检测小鸟与下方管子的碰撞
        if(this.detectCollisionWithBird(this.nextPipe.bottomPipe)){
            console.log("与下方碰撞")
            collideWithPipe=true;
        }
        //检测小鸟与地面的碰撞
        let collideWithGround=false;
        if(this.detectCollisionWithBird(this.ground)){
            console.log("与地面碰撞")
            collideWithGround=true;
        }
        //处理碰撞结果
        if(collideWithPipe||collideWithGround){
            if(collideWithGround){//与地面碰撞
                this.state=State.Dead;
            }else{//与水管碰撞
                this.state=State.Drop;
                this.runDropAction();
            }
            this.anim.stop();
            this.game.gameOver();
        }else{
            let birdLeft = this.node.x;
            let pipeRight = this.nextPipe.node.x+this.nextPipe.topPipe.width
            let crossPipe = birdLeft > pipeRight;
            if(crossPipe){
                this.game.gainScore();
                this.getNextPipe();

            }

        }
    },
    detectCollisionWithBird(otherNode){
        return cc.rectIntersectsRect(this.node.getBoundingBoxToWorld(),otherNode.getBoundingBoxToWorld());
    },
    fixBirdFinalPosition(){
        if(this.detectCollisionWithBird(this.ground)){
            this.node.y=this.ground.y+this.node.width/2;
        }
    }
});
